package com.gdqy.FCS.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.ClothingType;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.entity.Weave;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.ClothingTypeService;
import com.gdqy.FCS.service.UserService;
import com.gdqy.FCS.service.WeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("admin")
public class AdminController {
	@Autowired
	private UserService userService;
	@Autowired
	private ClothingTypeService clothingTypeService;
	@Autowired
	private WeaveService weaveService;
	
	@RequestMapping("admin")
	public ModelAndView admin(int user_id,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		//如果id跟session中的id对应，则跳转
		if(user_id == (int)session.getAttribute("user_id")) {
			modelAndView.setViewName("admin/adminIndividal");
		}else {
			String msg = "你不是管理员";
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("common/error");
		}
		return modelAndView;
	}
	
	/**************************用户管理*****************************/
	/*浏览用户列表*/
	@RequestMapping("scan_user")
	public ModelAndView scan_user(Args args,int pageindex, int pagesize){
		ModelAndView modelAndView = new ModelAndView();
		PageHelper.startPage(pageindex, pagesize);
		List<User> users = userService.selectAll();
		PageInfo<User> page = new PageInfo<User>(users);
		//分页对象
		ListResponse<User> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(users);
		modelAndView.addObject("json", json);
		modelAndView.setViewName("admin/user_list");
		return modelAndView;
	}
	/*启用用户账号*/
	@RequestMapping("start_user")
	public @ResponseBody String start_user(@RequestParam(value="user_id" ,required = true) int user_id){
		String msg = "fail";
		User user = userService.selectById(user_id);
		if(user!=null){
			user.setStatus(1);
			int date = userService.update(user);
			if(date>0){
				msg="success";
			}
		}
		return msg;
		
	}
	/*停用用户账号*/
	@RequestMapping("stop_user")
	public @ResponseBody String stop_user(@RequestParam(value="user_id" ,required = true) int user_id){
		String msg = "fail";
		User user = userService.selectById(user_id);
		if(user!=null){
			user.setStatus(0);
			int date = userService.update(user);
			if(date>0){
				msg="success";
			}
		}
		return msg;
	}
	
	
	
	
}
